<?php
session_start();
require_once '../config/database.php';
require_once '../includes/functions.php';

// Require login
requireLogin();

$user_id = $_SESSION['user_id'];
$orders = getOrdersByUser($pdo, $user_id);

// Check for order success message
$order_success = isset($_GET['order_success']) && $_GET['order_success'] == '1';
$order_id = isset($_GET['order_id']) ? $_GET['order_id'] : '';

$page_title = "My Orders";
?>
<?php include '../includes/header.php'; ?>

<main style="padding: 2rem 0;">
    <div class="container">
        <h1 style="margin-bottom: 2rem; color: #2c3e50;">My Orders</h1>

        <?php if ($order_success): ?>
            <div class="alert alert-success" style="margin-bottom: 2rem;">
                <i class="fas fa-check-circle"></i>
                <strong>Order Placed Successfully!</strong><br>
                Your order has been confirmed. Order ID: <strong><?php echo htmlspecialchars($order_id); ?></strong><br>
                You will receive an email confirmation shortly.
            </div>
        <?php endif; ?>

        <div style="display: grid; grid-template-columns: 1fr 2fr; gap: 2rem;">
            <!-- User Navigation -->
            <div class="card">
                <div class="card-header">
                    <h2>Account</h2>
                </div>
                <div style="display: flex; flex-direction: column; gap: 0.5rem;">
                    <a href="/chuan/535g/user/profile.php" class="btn btn-outline" style="text-align: left; justify-content: flex-start;">
                        <i class="fas fa-user"></i> Profile Information
                    </a>
                    <a href="/chuan/535g/user/orders.php" class="btn btn-primary" style="text-align: left; justify-content: flex-start;">
                        <i class="fas fa-shopping-bag"></i> My Orders
                    </a>
                    <a href="/chuan/535g/opencart_panel.php" class="btn btn-outline" style="text-align: left; justify-content: flex-start; background: #28a745; color: white; border-color: #28a745;">
                        <i class="fas fa-store"></i> OpenCart
                    </a>
                    <a href="/chuan/535g/auth/logout.php" class="btn btn-outline" style="text-align: left; justify-content: flex-start; color: #e74c3c;">
                        <i class="fas fa-sign-out-alt"></i> Logout
                    </a>
                </div>
            </div>

            <!-- Orders List -->
            <div>
                <?php if (empty($orders)): ?>
                    <div class="card" style="text-align: center; padding: 3rem;">
                        <i class="fas fa-shopping-bag" style="font-size: 4rem; color: #7f8c8d; margin-bottom: 1rem;"></i>
                        <h3>No orders yet</h3>
                        <p style="color: #7f8c8d; margin: 1rem 0 2rem;">Start shopping to see your orders here!</p>
                        <a href="/chuan/535g/products/" class="btn btn-primary">Start Shopping</a>
                    </div>
                <?php else: ?>
                    <div style="display: flex; flex-direction: column; gap: 1.5rem;">
                        <?php foreach ($orders as $order): ?>
                        <div class="card">
                            <div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 1rem;">
                                <div>
                                    <h3 style="margin: 0; color: #2c3e50;">
                                        <a href="/chuan/535g/user/order_details.php?id=<?php echo $order['id']; ?>" style="color: #e74c3c; text-decoration: none;">
                                            <?php echo htmlspecialchars($order['order_number']); ?>
                                        </a>
                                    </h3>
                                    <p style="margin: 0.5rem 0 0; color: #7f8c8d; font-size: 0.9rem;">
                                        Placed on <?php echo date('F j, Y', strtotime($order['created_at'])); ?>
                                    </p>
                                </div>
                                <div style="text-align: right;">
                                    <div style="font-size: 1.2rem; font-weight: bold; color: #2c3e50; margin-bottom: 0.5rem;">
                                        <?php echo formatPrice($order['total_amount']); ?>
                                    </div>
                                    <span class="status-badge status-<?php echo $order['status']; ?>">
                                        <?php echo ucfirst($order['status']); ?>
                                    </span>
                                </div>
                            </div>
                            
                            <div style="display: flex; justify-content: space-between; align-items: center; padding-top: 1rem; border-top: 1px solid #ecf0f1;">
                                <div style="color: #7f8c8d; font-size: 0.9rem;">
                                    <?php echo $order['item_count']; ?> item<?php echo $order['item_count'] != 1 ? 's' : ''; ?>
                                </div>
                                <div style="display: flex; gap: 1rem;">
                                    <a href="/chuan/535g/user/order_details.php?id=<?php echo $order['id']; ?>" class="btn btn-outline" style="padding: 0.5rem 1rem; font-size: 0.9rem;">
                                        View Details
                                    </a>
                                    <?php if ($order['status'] === 'delivered'): ?>
                                        <button class="btn btn-secondary" style="padding: 0.5rem 1rem; font-size: 0.9rem;">
                                            Reorder
                                        </button>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</main>

<?php include '../includes/footer.php'; ?>
